namespace ASPNETIdentity
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.CodeDom.Compiler;
    using Eco.ObjectImplementation;
    using Eco.ObjectRepresentation;
    using Eco.Services;
    using Eco.Subscription;
    using Eco.UmlCodeAttributes;
    using Eco.UmlRt;


    public partial class SysSingleton
    {
        [UmlElement(Id = "a1468638-9271-457b-a100-8bfca7d27db5"), UmlMetaAttribute("isQuery", true)]
        public SysUser GetCurrentSysUser()
        {
            return _CurrentSysUser;
        }

        private SysUser _CurrentSysUser;
        public static void InstallCurrentUser(string id, IEcoServiceProvider sp)
        {
            sp.GetEcoService<IAsyncSupportService>().PerformTaskNowIfInAsyncThread(() =>   // If we are in WPF with Async on this will be good
            {
                var sing = sp.GetEcoService<IOclService>().Evaluate("SysSingleton.oclSingleton").GetValue<SysSingleton>();
                if (string.IsNullOrEmpty(id))
                {
                    sing._CurrentSysUser = null;
                    return;
                }
                var list = sp.GetEcoService<IOclPsService>().Execute("SysUser.allinstances->select(u|u.Id='" + id + "')").GetAsIList<SysUser>();
                if (list.Count > 0)
                    sing._CurrentSysUser = list[0];
                else
                    sing._CurrentSysUser = null;
            });
        }
    }
}